#ifndef TOFTLIB_NET_ASYNCTCPCLIENT_INCLUDED
#define TOFTLIB_NET_ASYNCTCPCLIENT_INCLUDED

#include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>

namespace toftlib
{
    class TcpClient
    {
    public:
        TcpClient() : boostSocket(ioService)
        {
        }
        virtual ~TcpClient()
        {
        }
        void Connect(const char* destination, unsigned int port)
        {
            boost::asio::ip::tcp::resolver resolver(ioService);
            boost::asio::ip::tcp::resolver::query query(destination, boost::lexical_cast<std::string>(port));
            boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query);
            boost::asio::ip::tcp::resolver::iterator end;

            boost::system::error_code error = boost::asio::error::host_not_found;
            while (error && iterator != end)
            {
                boostSocket.close();
                boostSocket.connect(*iterator++, error);
            }

            if (error)
            {
                throw boost::system::system_error(error);
            }
        }

    protected:

        void CloseClient()
        {
            boostSocket.close();
        }

        boost::asio::io_service ioService;
        boost::asio::ip::tcp::socket boostSocket;
        boost::system::error_code lastError;
    };
}

#endif
